<?php

class Admin_Table_Clubs extends Apk_Db_Table
{
	protected $_name      = 'clubs';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {    	
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title', 'add_date','is_active', 'is_top'))   
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');    	

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)		
			{					
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;
				case 'is_top':	
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));							
					break;	
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);		
			}				
		}		 
			
    		  
		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.title');					
    		  
    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);    	

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }
    
    
    public function GetCommentsList($id, $page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_comments', array('id', 'text', 'add_date'))   
    		  ->where('id_article = ?', $id);
    		  

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)		
			{				
				default:
					if(intval($v) > 0)
					$sel->where('articles_comments.'.$k.' = ?', $v);		
			}				
		}		
    		  
		if(!is_null($sort) && strlen($sort) > 0) $sel->order('articles_comments.'.$sort);
		else $sel->order('articles_comments.add_date desc');	
    		  
    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }  
    
    public function GetCommentsRow($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('articles_comments')   
    		  ->where('id = ?', $id);    		  		    		  
    		  
    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }        
    
    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {    	
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {    	
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;     	

    	if(!isset($data['is_active'])) $data['is_active'] = 0;  	    	    	
    	if(!isset($data['is_show_gallery'])) $data['is_show_gallery'] = 0;  	
    	
    	if(!isset($data['is_fight_room'])) $data['is_fight_room'] = 0;  	
    	if(!isset($data['is_yoga'])) $data['is_yoga'] = 0;  	
    	if(!isset($data['is_children_fitness'])) $data['is_children_fitness'] = 0;  	
    	if(!isset($data['is_ping_pong'])) $data['is_ping_pong'] = 0;  	
    	if(!isset($data['is_playing_field'])) $data['is_playing_field'] = 0;  	
    	if(!isset($data['is_sport_doctor'])) $data['is_sport_doctor'] = 0;  	
    	if(!isset($data['is_spa'])) $data['is_spa'] = 0;  	
    	if(!isset($data['is_sauna'])) $data['is_sauna'] = 0;  	
    	if(!isset($data['is_restaurant'])) $data['is_restaurant'] = 0;  	
    	if(!isset($data['is_children_room'])) $data['is_children_room'] = 0;  	
    	if(!isset($data['is_car_park'])) $data['is_car_park'] = 0;  	
    	
		return parent::update($data, 'id = '.$id);
    }
    
    public function update_all(array $data, $id, $user_id)
    {    	
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;
    	
		return parent::update($data, 'id = '.$id);
    }    

    public function remove($items, $user_id)
    {    	
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id; 
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;
	    	
	    	parent::update($data, 'id = '.$id);    		
    	}
    	
    	return true;    	
	}
	
    public function remove_commetns($items, $aid)
    {    	
    	foreach($items as $k => $id)
    	{	    	
	    	$this->getAdapter()->delete('articles_comments', 'id = '.$id);    		
    	}
    	
    	$this->commetns_count($aid);
    	
    	return true;    	
	}	
	
    public function commetns_count($id)   
    {
    	$sel = $this->getAdapter()->select()
    					->from('articles_comments', array('count' => 'count(id)'))
    					->where('id_article = ?', $id);
    	$count = $this->getList($sel)->getRow(0)->toArray();  
    	
    	$d['comments_count'] = $count['count'];
    	$this->update($d, 'id = '.$id);  	
    }
    
	public function comment_insert(array $data, $user_id = 0)    
	{    	
		$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	
        $this->getAdapter()->insert('articles_comments', $data);
        return $this->getAdapter()->lastInsertId('articles_comments');
    }
    
	public function comment_update(array $data, $id, $user_id = 0)    
	{    	
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;
    	
        return $this->getAdapter()->update('articles_comments', $data, 'id = '.$id);        
    } 

    public function photo_count($id)   
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects', array('count' => 'count(id)'))    					
    					->where('is_active')
    					->where('is_ghost = 0')
    					->where('is_removed = 0')
    					->where('id_article = ?', $id);    					
    					
    	$count = $this->getList($sel)->getRow(0)->toArray();  
    	
    	$d['count_photo'] = $count['count'];
    	parent::update($d, 'id = '.$id);  	
    }     		
	    	

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>